<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>

	<body>
		<span >经典案例</span><br />
		 <span>字符串==>数组</span> <br />
		 <span>字符串==>对象</span> <br />
		 <span>数组==>字符串</span> <br />
		 <span>对象===>字符串</span> <br />
		 <span>数组===>对象</span> <br />
		  <span>对象===>数组</span> <br />

		<script type="text/javascript">
			var url0 = "http://www.kky365.vip/addGet?name=kky&age=18&sex=0&job=&hobby";
			console.log("url0:", url0)
			var r1 = myUrl2Arr(url0); //[["name","kky"],["age",18],["sex",0],["job",""],["hobby"]];//二/多维数组
			console.log("r1:", r1);
			console.log("////////////////");

			var r2 = myUrl2Obj(url0); //{name:"kky",age:18,sex:0,job:"",hobby:undefined}
			console.log("r2:", r2);

			/*
			函数
			1.函数的功能
			2.是否有参数,有几个
			3.是否有返回值
			*/

			//把字符串转换成数组
			function myUrl2Arr(url) {
				var arr1 = url.split("?");
				//console.log("arr1:",arr1);
				var str1 = arr1[1];
				//console.log("str1:",str1);
				var arr2 = str1.split("&");
				//console.log("arr2:",arr2);
				var arr0 = [];
				for(var i = 0; i < arr2.length; i++) {
					//console.log("arr2["+i+"]:",arr2[i]);
					var arr3 = arr2[i].split("=");
					//console.log("arr3:",arr3);
					arr0.push(arr3)
				}
				return arr0;
			}

			//字符串转换成对象
			function myUrl2Obj(url) {
				var arr1 = url.split("?");
				//console.log("arr1:",arr1);
				var str1 = arr1[1];
				//console.log("str1:",str1);
				var arr2 = str1.split("&");
				//console.log("arr2:",arr2);
				var obj = {};
				for(var i = 0; i < arr2.length; i++) {
					//console.log("arr2["+i+"]:",arr2[i]);
					var arr3 = arr2[i].split("=");
					//console.log("arr3:",arr3);
					//console.log("属性名:arr3[0]:",arr3[0],"属性值arr3[1]:",arr3[1]);
					obj[arr3[0]] = arr3[1];
				}
				return obj;
			}

			// var obj={};
			// obj["user"]="张倩";
			// var arr=["age",18]
			// // var str="age";
			// // obj[str]=18;
			// console.log("99999999obj:",obj);
			// obj[arr[0]]=arr[1];

			/*
			
			
			*/
			var arr = [
				["name", "kky"],
				["age", 18],
				["sex", 0],
				["job", ""],
				["hobby"]
			];
			var obj = {
				name: "kky",
				age: 18,
				sex: 0,
				job: "",
				hobby: undefined
			};

			var url1 = "http://www.kky365.vip/addGet?";
			var url2 = "http://www.kky365.vip/addGet?";
			console.log("////////////////");
			var r3 = myArr2Str(arr);
			console.log("r3:", r3);
			console.log("////////////////");
			var r4 = myObj2Str(obj);
			console.log("r4:", r4);

			//把数组转换成字符串
			function myArr2Str(arr) {
				var str = "";
				for(var i = 0; i < arr.length; i++) {
					//console.log("arr["+i+"]:",arr[i]);
					if(i == arr.length - 1) {
						var str1 = arr[i].join("=");
					} else {
						var str1 = arr[i].join("=") + "&";
					}
					// console.log("1111str1:",str1);
					str += str1;
				}
				url1 += str;
				return url1;
			}

			//把对象转换成字符串
			function myObj2Str(obj) {
				var str = "";
				for(var o in obj) {
					// console.log("o:",o,"obj["+o+"]:",obj[o]);
					if(o == "hobby") {
						var str1 = o;
					} else {
						var str1 = o + "=" + obj[o] + "&";
					}

					// console.log("222222222str1:",str1);
					str += str1;
				}
				url2 += str;
				return url2;
			}

			/*
		   
		   
			*/

			var arr11 = [
				["name", "kky"],
				["age", 18],
				["sex", 0],
				["job", ""],
				["hobby"]
			];
			var obj11 = {
				name: "kky",
				age: 18,
				sex: 0,
				job: "",
				hobby: undefined
			};
			var r11 = myArr2Obj(arr11);
			console.log("r11:", r11);

			console.log("//////////");

			var r22 = myObj2Arr(obj11);
			console.log("r22:", r22);

			//把数组转换成对象
			function myArr2Obj(arr) {
				var obj = {};
				for(var i = 0; i < arr.length; i++) {
					//console.log("3333arr["+i+"]:",arr[i],"属性名:",arr[i][0],"属性值:",arr[i][1]);
					obj[arr[i][0]] = arr[i][1];
				}
				return obj;
			}

			//把对象转换成数组
			function myObj2Arr(obj) {
				var arr = [];
				for(var o in obj) {
					console.log("o:", o, "obj[" + o + "]:", obj[o]);
					var arr2 = [];
					arr2.push(o, obj[o]);
					arr.push(arr2);
				}
				return arr;
			}




			//			Date中的不明白的
			console.group("3几天后是什么时间");;
			(function() {
				var d1 = new Date();
				var t1 = d1.getTime();
				var t2 = t1 + 3 * 24 * 3600 * 1000;

				var d2 = new Date(t2); //通过新的时间戳,获取指定的时间对象
				console.log("d2:", d2);
				var year = d2.getFullYear();
				var month = d2.getMonth() + 1;
				var date = d2.getDate();
				var day = d2.getDay();
				console.log("300天后是", year, "年", month, "月", date, "日", "周", day);
			}());
			console.groupEnd();




     
			console.group("4 parse() 返回1970年1月1日午夜到指定日期(字符串)的毫秒数");;
			(function() {
				var p1 = Date.parse("2020-06-08");
				console.log("p1:", p1);
				var p2 = Date.parse("2020-06-09");
				console.log("p2:", p2);
				var p3 = (p2 - p1);
				console.log("p3:", p3, "秒数");

				var seconds = p3 / 1000;
				console.log("seconds:", seconds);
				var hours = seconds / 3600;
				console.log("hours:", hours);

			}());
			console.groupEnd();

//     练习自己手敲

           //把字符串转换成数组
         
           //字符串转换成对象
		   //数组转换成字符串
		   //对象转换成字符串
		   //数组转换成对象
		   //对象转换成数组


			
		</script>
	</body>

</html>